package controller

import (
	"github.com/gin-gonic/gin"
	"main/service"
	"main/tool"
)

type ShopController struct {

}


func (sc *ShopController) Router(engine *gin.Engine) {
	engine.GET("/api/shops", sc.GetShopList)
	engine.GET("/api/search_shops", sc.SearchShopList)
}

func (sc *ShopController) SearchShopList(ctx *gin.Context) {
	longitude := ctx.Query("longitude")
	latitude := ctx.Query("latitude")

	keyword := ctx.Query("keyword")
	if keyword == "" {
		tool.Failed(ctx, "请重新输入商铺名称")
		return
	}

	// 执行搜索逻辑
	ss := &service.ShopService{}
	shops, err := ss.SearchShops(longitude, latitude, keyword)
	if err != nil {
		tool.Failed(ctx, "get shop list: " + err.Error())
		return
	}
	tool.Success(ctx, shops)
}

func (sc *ShopController) GetShopList(ctx *gin.Context) {
	longitude := ctx.Query("longitude")
	latitude := ctx.Query("latitude")

	if longitude == "" || latitude == "" {
		longitude = "123.25"
		latitude = "55.34"
	}

	// 查询附件商家, service
	ss := service.ShopService{}
	shops, err := ss.ShopList(longitude, latitude)
	if err != nil {
		tool.Failed(ctx, "get shop list: " + err.Error())
		return
	}

	// 查询关联的商户信息
	if len(shops) >= 0 {
		for i, shop := range shops {
			shopServices := ss.GetService(shop.Id)
			if len(shopServices) == 0 {
				shop.Supports = nil
			} else {
				shop.Supports = shopServices
			}
			shops[i] = shop
		}
	}
	tool.Success(ctx, shops)
}
